package com.c.ctgproject.ui.doorkeeper.part

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.c.ctgproject.base.BaseViewModel
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.RequestBody

class DoorkeeperPartViewModel : BaseViewModel() {


    private val mListResult = MutableLiveData<String>()
    val listResult: LiveData<String> get() = mListResult

    private val mConfirmResult = MutableLiveData<String>()
    val confirmResult: LiveData<String> get() = mConfirmResult

    private val mAllSeller = MutableLiveData<String>()
    val allSeller: LiveData<String> get() = mAllSeller

    fun getAllSeller() {
        MyLog.d("获取销售人员")
        launch({
            mAllSeller.value = ApiMethods.getApiService().getAllSeller(userToken).awaitSync()
        }, {
            mError.value = it.message.toString()
        })
    }


    fun getPartOutList(page: Int, size: Int, partsSalesNo: String = "", createPersonId: Int = 0) {
        launch({
            val map: MutableMap<String, Any> = java.util.HashMap()
            map["pageSize"] = size
            map["pageNum"] = page

            if (partsSalesNo != "")
                map["partsSalesNo"] = partsSalesNo
            if (createPersonId != 0)
                map["createPersonId"] = createPersonId

            val obj = Gson().toJson(map)
            val body = RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                obj
            )
            mListResult.value = ApiMethods.getApiService().doorkeeperPartOutList(userToken, body).awaitSync()
        }, {
            mError.value = it.message.toString()
        })
    }

    //确认配件出场
    fun confirmPartOut(orderId: Int) {
        MyLog.d("订单ID:$orderId")
        launch {
            mConfirmResult.value = ApiMethods.getApiService().doorkeeperPartOutConfirm(userToken, orderId).awaitSync()
        }
    }

}